package logic

import (
	"context"
	"fmt"
	"github.com/google/uuid"
	"good_srv/good"
	"good_srv/internal/svc"
	"good_srv/models"
	"good_srv/pkg"
	"strconv"

	"github.com/zeromicro/go-zero/core/logx"
)

type OrderAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewOrderAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *OrderAddLogic {
	return &OrderAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 订单列表展示
func (l *OrderAddLogic) OrderAdd(in *good.OrderAddRequest) (*good.OrderAddResponse, error) {
	// todo: add your logic here and delete this line
	var err error

	var orders models.Order
	var og models.OrderGood
	orders.OrderSn = uuid.NewString()
	orders.Stock = in.Num
	orders.Amount = strconv.FormatInt(in.Num*int64(og.Price), 10)
	orders.Status = 0
	orders.PayType = 0

	if err = orders.OrderAdd(); err != nil {
		logx.Error("订单表添加失败")
		return nil, fmt.Errorf("订单表添加失败")
	}
	if err = og.OrderGoodAdd(); err != nil {
		logx.Error("订单商品表添加失败")
		return nil, fmt.Errorf("订单商品表添加失败")
	}
	amount := fmt.Sprintf("%.2f", orders.Amount)
	payurl := pkg.Alipay(orders.OrderSn, amount)
	return &good.OrderAddResponse{
		PayURL: payurl,
	}, nil
}
